<?php

class Admin_ResourceController extends Zend_Controller_Action
{	
    private $_params;
	private $_auth;
	private $_acl;
    public function init(){
        $this->_params      = $this->_request->getParams();        
        $this->_acl         = new My_Acl();
        $this->view->acl    = $this->_acl;        
        $this->_auth        = $this->_acl->checkAllowed();
        $this->view->auth   = $this->_auth;
        $this->view->headScript()->appendFile(BASE_URL.'static/assets/js/main/resource.js');
    }
    public function indexAction(){       
        $modelResource              = new Admin_Model_Resource;
        $resourceList               = $modelResource->getAllData()->toArray();
        $adminResource = $modelResource->getData(array('moduleName'=>'admin'));
        $defaultResource = $modelResource->getData(array('moduleName'=>'default'));        
        $adControl = array();
        $adMenu = array();
        foreach($adminResource as $re){
            if(!in_array($re['controllerName'], $adControl))
                $adControl[] = $re['controllerName'];            
            if(!in_array($re['menuModel'], $adMenu))
                $adMenu[] = $re['menuModel'];            
        }                
        $this->view->adCtrlResourceList   = $adControl;
        $this->view->adMenuResourceList   = $adMenu;                                
    }
    public function addAction(){   
        $form = new Admin_Form_Resource();
        $form->submit->setLabel('Thêm mới');
        $this->view->form = $form;
        if($this->getRequest()->isPost()){
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)){
                $addData['resourceName']           = $form->getValue('resourceName');
                $addData['moduleName']             = strtolower($form->getValue('moduleName'));
                $addData['controllerName']         = strtolower($form->getValue('controllerName'));
                $addData['actionName']             = strtolower($form->getValue('actionName'));
                $addData['menu']                   = $addData['moduleName'].'/'.$addData['controllerName'].'/'.$addData['actionName'];
                $addData['detail']                 = $form->getValue('detail');
                $addData['controllerNameShowup']   = $form->getValue('controllerNameShowup');
                $addData['actionNameShowup']       = $form->getValue('actionNameShowup');
                $addData['menuName']               = $form->getValue('menuName');
                $addData['menuModel']              = $form->getValue('menuModel');                                    
                $addData['ord']                    = $form->getValue('ord');
                $addData['displayTemplate']        = $form->getValue('displayTemplate');                
                
                $modelResource = new Admin_Model_Resource();
                $resourceCheck = $modelResource->getDataCheckUnique(array('moduleName'=>$addData['moduleName'],'controllerName'=>$addData['controllerName'],'actionName'=>$addData['actionName']));
                  
                if(!$newsCatCheck){                    
                    $resourceId = $modelResource->addData($addData);
                    if($resourceId){
                        $this->_helper->redirector('index', 'resource', 'admin');
                    }else{
                        $this->view->mess = "Đã có lỗi xảy ra. Vui lòng thử lại!";
                        $this->view->flag = "error";
                   }
                }else{
                    $this->view->mess = "Resource đã tồn tại !";
                    $this->view->flag = "error"; 
                }                				
            }
        }                                       
    }
    public function editAction(){        
        $resourceId = (int)($this->_getParam('resourceId', 0));        
        $modelResource = new Admin_Model_Resource();        
        $resourceCheck = $modelResource->getDataByResourceId($resourceId);
        $form = new Admin_Form_Resource();
        $this->view->form = $form;                       
        if($resourceCheck){                  
            if($this->getRequest()->isPost()){
                $formData = $this->getRequest()->getPost();                 
                if ($form->isValid($formData)) {
                    $editData['resourceId']             = $form->getValue('resourceId');
                    $editData['resourceName']           = $form->getValue('resourceName');
                    $editData['moduleName']             = strtolower($form->getValue('moduleName'));
                    $editData['controllerName']         = strtolower($form->getValue('controllerName'));
                    $editData['actionName']             = strtolower($form->getValue('actionName'));
                    $editData['menu']                   = $editData['moduleName'].'/'.$editData['controllerName'].'/'.$editData['actionName'];
                    $editData['detail']                 = $form->getValue('detail');
                    $editData['controllerNameShowup']   = $form->getValue('controllerNameShowup');
                    $editData['actionNameShowup']       = $form->getValue('actionNameShowup');
                    $editData['menuName']               = $form->getValue('menuName');
                    $editData['menuModel']              = $form->getValue('menuModel');                                    
                    $editData['ord']                    = $form->getValue('ord');
                    $editData['displayTemplate']        = $form->getValue('displayTemplate');                                
                    //pr($editData);           
                    $modelNewsCat = new Admin_Model_Resource();                
                    $resourceCheck = $modelNewsCat->getDataCheckUnique(array('moduleName'=>$editData['moduleName'],'controllerName'=>$editData['controllerName'],'actionName'=>$editData['actionName']));
                                                  
                    if(!$resourceCheck || $editData['resourceId'] == $resourceCheck['resourceId']){                        
                        $modelNewsCat->updateData($editData['resourceId'],$editData);                        
                        $this->_helper->redirector('index', 'resource', 'admin');                        
                    }else{
                        $this->view->mess = "Tên hoặc Mã danh mục đã tồn tại. Vui lòng chọn tên khác!";
                        $this->view->flag = "error"; 
                    }                				
                }
            }else{                  
                //pr($resourceCheck->toArray());
                $form->populate($resourceCheck->toArray());                
            }
        }else{
            $this->_helper->redirector('index', 'newscat', 'admin');
        }
    }
    public function deleteAction(){
        if($this->getRequest()->isPost()){
            $del = $this->getRequest()->getPost('del');
            if($del == 'Có'){               
                $resourceId = $this->getRequest()->getPost('resourceId');
                $modelResource = new Admin_Model_Resource();                                                                                                
                $modelResource->deleteData($resourceId);                
                // ghi log
            }
            $this->_helper->redirector('index', 'resource', 'admin');
        }else{
            $id = $this->_getParam('resourceId', 0);
            $modelResource = new Admin_Model_Resource();            
            $this->view->resourceCheck = $modelResource->getDataByResourceId($id);
        }
    }
    
    public function displayAction(){
        $this->_helper->layout->disableLayout();                        
        if($this->getRequest()->isPost()){
            if(isset($this->_params['act']) && $this->_params['act'] == 'display' && isset($this->_params['resourceId'])){            
                $modelResource = new Admin_Model_Resource();                
                $resourceCheck = $modelResource->getDataByResourceId($this->_params['resourceId']);        
                if($resourceCheck){
                    if($resourceCheck['displayTemplate'] == 1)
                        $modelResource->updateData($this->_params['resourceId'],array('displayTemplate'=>0));
                    else
                        $modelResource->updateData($this->_params['resourceId'],array('displayTemplate'=>1));                                 
                }else{                    
                    return false;    
                }
            }                
        }else{
            $this->_helper->redirector('index', 'resource', 'admin');
        }               
    }
    public function ordAction(){
        $this->_helper->layout->disableLayout();                        
        if($this->getRequest()->isPost()){
            if(isset($this->_params['act']) && $this->_params['act'] == 'ord' && isset($this->_params['resourceId'])){            
                $modelResource = new Admin_Model_Resource();                
                $resourceCheck = $modelResource->getDataByResourceId($this->_params['resourceId']);        
                if($resourceCheck){                    
                    $modelResource->updateData($this->_params['resourceId'],array('ord'=>(int)$this->_params['ord']));                                                     
                }else{                    
                    return false;    
                }
            }                
        }else{
            $this->_helper->redirector('index', 'resource', 'admin');
        }
    }
}